package com.dragon.mmochat.model.scheduleJob.multiServer;

import java.awt.Color;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.quartz.Job;
import org.quartz.JobExecutionContext;

import com.dragon.mmochat.MmochatConstant;
import com.dragon.mmochat.model.MmochatTeam;
import com.dragon.mmochat.model.battle.MmochatBattle;
import com.dragon.mmochat.model.map.MmochatMap;
import com.dragon.mmochat.model.role.MmochatPlayer;
import com.dragon.mmochat.service.MmochatBattleService;
import com.dragon.mmochat.service.MmochatChatService;
import com.dragon.mmochat.service.MmochatFriendService;
import com.dragon.mmochat.service.MmochatMainService;
import com.dragon.mmochat.service.MmochatMapService;
import com.dragon.mmochat.service.MmochatMultiServerPkService;
import com.dragon.mmochat.service.MmochatTeamService;
import com.dragon.mmochat.util.MmochatUtil;

public class MultiServerLeiTai_Clear implements Job {

	@Override
	public void execute(JobExecutionContext context) {
		try {
			// 跨服擂台清人
			for (MmochatPlayer me : MmochatMainService.players.values()) {
				try {
					// 停止战斗
					if (me.isInBattle()) {
						MmochatBattle battle = MmochatBattleService.battles
								.get(me.getBattleId());
						if (battle != null) {
							battle.closeBattle();
						}
					}
					// 离队
					if (me.isInTeam()) {
						MmochatTeam team = MmochatTeamService.teams.get(me
								.getTeamId());
						team.leaveTeam(me.getRoleId());
					}

					Date now = new Date();
					SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
					String curTimeString = sdf.format(now);
					String msg = MmochatUtil.wrapColor("跨服擂台目前已经关闭，请退出...",
							Color.green)
							+ "跨服擂台开放时间:0:00-19:00"
							+ "和23:00-24:00,当前服务器时间:"
							+ curTimeString;

					MmochatChatService.sendSystemMsgToPerson(me, msg);
					MmochatFriendService.sendSystemMsgToPersonByFriendMessage(
							me, msg);

					// 从本跨服服务器中清除
					MmochatMainService.leaveGame(me, false);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			try {
				MmochatMap map = MmochatMapService.maps
						.get(MmochatConstant.mapId_跨服擂台);
				if (map != null) {
					for (int i = 0; i < MmochatConstant.maxMapLineNum; i++) {
						map.getLineRoleMap(i).clear();
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			MmochatBattleService.battles.clear();
			MmochatMainService.players.clear();
			MmochatMainService.players_skyIdMap.clear();
			MmochatMainService.offlinePlayers.clear();
			MmochatMainService.offlinePlayers_skyIdMap.clear();
			MmochatMainService.nameMap.clear();
			MmochatMultiServerPkService.tmpUsers.clear();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
